﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KiDS_Projekat
{
	/// <summary>
	/// Klasa koja predstavlja REGISTER_SERVENT_COMMAND
	/// </summary>
	public class RegisterServentCommand : Command
	{
		private string _message;
		private Node _myNode;
		private string _address;
		public RegisterServentCommand(SOCKET socket, string address, string message, Node myNode)
			: base(socket)
		{
			_address = address;
			_myNode = myNode;
			_message = message;
		}

		public override void DoCommmand()
		{
			var servents = _message.Split(';');
			foreach (var item in servents)
			{
				//lokacija:master:port
				var parameters = item.Split(':');
				parameters[0] = CommandFactory.NormalizeLocation(parameters[0]);
				if (parameters[1] == "0")
				{
					Slave slave = new Slave(_address, (ushort)Int16.Parse(parameters[2]), parameters[0]);
					_myNode.AddSlave(slave.Location, slave);
				}
				else
				{
					Master master = new Master(_address, (ushort)Int16.Parse(parameters[2]), parameters[0]);
					_myNode.AddMaster(master.Location, master);
				}
			}
			Native.SendMessage(CMD.CMD_REPLY_OK + "", _socket);
		}

	}
}
